<?php

require_once('db_config.php');
require_once('Threads.php');

// Connect to the database (using db_config.php)

$db = db_connect();

// Setting these values determines what data will go into "Test_Threads"

$annotator = 1;
$filenumber = 1;

// Get the relevant data from "Test_Annotations",
// which now has the data migrated from "Annotations"

$tree = '';
$array = array();
$array["children"] = array();
$threads = mysql_query("SELECT filenumber, annotator, postnum, parent, thread
                        FROM $annotationsTable
                        WHERE filenumber = $filenumber
                        AND annotator = $annotator")
           or die(__FILE__ . ":" . __LINE__ .
                  ": Couldn't execute thread query." . mysql_error());

// Build a "threads" array from the queried data

$array["roots"] = array();
while($row = mysql_fetch_array($threads, MYSQL_ASSOC))
{ if($row["parent"] == "-1")
  {  array_push($array["roots"], $row["postnum"]); 
     $array["children"][$row["postnum"]] = array();
  }
  else
  {
     if(is_array($array["children"][$row["parent"]]) == false)
     {  $array["children"][$row["parent"]] = array(); }
     array_push($array["children"][$row["parent"]], $row["postnum"]);
  }
}

// Iterate over the thread roots, make a lisptree representation using
// each root, and insert each lisptree into the "Test_Threads" table

reset($array["roots"]);
foreach($array["roots"] as $val)  // $val = thread root
{  $lisptree = makeLispTree($array["children"], "$val"); 
                 // makeLispTree is in Threads.php
   mysql_query("INSERT INTO `Test_Threads` VALUES (" .
               $filenumber . ", " .
               $annotator . ", " .
               (int)$val . ", " .
               "'" . $lisptree . "')");
}

?>
